package com.google.gwt.sample.dynatablerf.client.presenters;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.sample.dynatablerf.client.events.CloseServicoListEvent;
import com.google.gwt.sample.dynatablerf.client.events.ServicoSelectionChangeEvent;
import com.google.gwt.sample.dynatablerf.shared.GrifoRequestFactory;
import com.google.gwt.sample.dynatablerf.shared.GrifoRequestFactory.ServicoProjetoRequest;
import com.google.gwt.sample.dynatablerf.shared.GrifoSharedConstants;
import com.google.gwt.sample.dynatablerf.shared.GrifoSharedConvertUtil;
import com.google.gwt.sample.dynatablerf.shared.ProjetoProxy;
import com.google.gwt.sample.dynatablerf.shared.ServicoProjetoProxy;
import com.google.gwt.sample.dynatablerf.shared.ServicoProjetoQueryParamsProxy;
import com.google.gwt.sample.dynatablerf.shared.ServicoProxy;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle.MultiWordSuggestion;
import com.google.gwt.user.client.ui.SuggestOracle;
import com.google.gwt.user.client.ui.SuggestOracle.Callback;
import com.google.gwt.user.client.ui.SuggestOracle.Request;
import com.google.gwt.user.client.ui.SuggestOracle.Response;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
import com.google.web.bindery.requestfactory.shared.Receiver;

public class ServicoProjetoListPresenter
		extends
		MultipleSelectionDataPresenter<ServicoProjetoProxy, ServicoProjetoRequest> {

	class ServicoWordSuggestion extends MultiWordSuggestion {

		private ServicoProxy servicoProxy;

		public ServicoWordSuggestion(ServicoProxy servicoProxy) {
			super("", servicoProxy.getCodigo() + " - "
					+ servicoProxy.getDescricao());
			this.servicoProxy = servicoProxy;
		}

		public ServicoProxy getServicoProxy() {
			return servicoProxy;
		}

	}

	private MultipleSelectionDataDisplay<ServicoProjetoProxy> display;
	private ProjetoProxy projetoProxy;
	private int numServicoProjeto;

	public ServicoProjetoListPresenter(
			MultipleSelectionDataDisplay<ServicoProjetoProxy> display,
			ProjetoProxy projetoProxy, GrifoRequestFactory requestFactory,
			EventBus eventBus) {
		super(eventBus, requestFactory);
		this.display = display;
		this.projetoProxy = projetoProxy;
	}

	@Override
	protected MultipleSelectionDataDisplay<ServicoProjetoProxy> getMultipleSelectionDataDisplay() {
		return display;
	}

	@Override
	protected void onMultipleSelection(Set<ServicoProjetoProxy> selectedSet) {
		getEventBus().fireEvent(
				new ServicoSelectionChangeEvent(selectedSet, projetoProxy));

	}

	@Override
	protected void fetch(final int start, int length) {

		ServicoProjetoRequest servicoProjetoRequest = getRequestContext();

		ServicoProjetoQueryParamsProxy paramsProxy = servicoProjetoRequest
				.create(ServicoProjetoQueryParamsProxy.class);
		paramsProxy.setProjeto(projetoProxy);

		servicoProjetoRequest.findAll(paramsProxy, start, length)
				.with("servico")
				.fire(new Receiver<List<ServicoProjetoProxy>>() {

					@Override
					public void onSuccess(List<ServicoProjetoProxy> response) {
						if (response.size() == 0) {
							getAsyncDataProvider().updateRowCount(0, true);

						} else {
							getAsyncDataProvider().updateRowCount(
									numServicoProjeto, true);

						}
						getAsyncDataProvider().updateRowData(start, response);
					}
				});

	}

	@Override
	protected void fireCloseWindowEvent() {
		getEventBus().fireEvent(new CloseServicoListEvent(projetoProxy));

	}

	@Override
	protected void fetch() {

		ServicoProjetoRequest servicoProjetoRequest = getRequestContext();

		ServicoProjetoQueryParamsProxy paramsProxy = servicoProjetoRequest
				.create(ServicoProjetoQueryParamsProxy.class);

		paramsProxy.setProjeto(projetoProxy);

		servicoProjetoRequest.count(paramsProxy).fire(new Receiver<Long>() {

			@Override
			public void onSuccess(Long response) {
				numServicoProjeto = GrifoSharedConvertUtil
						.converToInteger(response);

			}
		});

	}

	@Override
	protected void fetchSuggestions(final Request request,
			final Callback callback) {
		ServicoProjetoRequest servicoProjetoRequest = getRequestContext();

		ServicoProjetoQueryParamsProxy paramsProxy = servicoProjetoRequest
				.create(ServicoProjetoQueryParamsProxy.class);
		paramsProxy.setProjeto(projetoProxy);

		servicoProjetoRequest
				.findAll(request.getQuery(), paramsProxy,
						GrifoSharedConstants.SUGGESTION_MAX_RESULT)
				.with("servico")
				.fire(new Receiver<List<ServicoProjetoProxy>>() {

					@Override
					public void onSuccess(List<ServicoProjetoProxy> response) {

						List<Suggestion> suggestions = new ArrayList<SuggestOracle.Suggestion>();
						if (response != null) {
							for (ServicoProjetoProxy ServicoProjetoProxy : response) {
								suggestions.add(new ServicoWordSuggestion(
										ServicoProjetoProxy.getServico()));
							}
						}
						callback.onSuggestionsReady(request, new Response(
								suggestions));
					}
				});

	}

	@Override
	protected void onFetchSelection(SelectionEvent<Suggestion> event) {
		// TODO Auto-generated method stub

	}

	@Override
	protected ServicoProjetoRequest getRequestContext() {
		return getRequestFactory().servicoProjetoRequest();
	}

}